package com.platform.comm.util;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.comm.web.results.JsonResult;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description: 通用方法
 *
 * @author libin
 * Created on 2019/4/24
 **/
public class CommonWriter {

    public static void write(JsonResult result, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        try (ServletOutputStream out = response.getOutputStream()) {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(out, JsonEncoding.UTF8);
            objectMapper.writeValue(jsonGenerator, result);
        }
    }

}
